﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxPackage.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxPackage.cs
// 创建时间：2014-12-25 12:58
// 修改时间：2014-12-23 15:47
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System.Text;
using Microsoft.Win32;
using WebCommon;

namespace Payment.Weixin2
{
    #region

    using System;
    using System.Collections.Generic;

    #endregion

    /// <summary>
    /// The wx package.
    /// </summary>
    public class WxPackage
    {

        private string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WxPackage"/> class.
        /// </summary>
        /// <param name="payOrder">
        ///     The pay Service.
        /// </param>
        /// <param name="wxConfig">
        ///     The wx config.
        /// </param>
        /// <param name="spbillCreateIp">ip地址</param>
        /// <param name="nonceStr">随即字符串</param>
        public WxPackage(IPayOrder payOrder, WxConfig wxConfig, string spbillCreateIp, string nonceStr)
        {
            this.NotifyUrl = wxConfig.NotifyUrl;//URL
            this.SpbillCreateIp = "";//IP
            this.OutTradeNo = payOrder.GetId();
            this.Appid = wxConfig.AppId;
            this.Mchid = wxConfig.Mchid;
            this.TotalFee = payOrder.GetTotalFeeInt().ToString(); ;
            this.Body = payOrder.GetSubject();
            this.NonceStr = "";
           
        }

        #endregion

        /// <summary>
        /// 公众ID
        /// </summary>
        public string Appid { get; set; }

        /// <summary>
        /// 商户id
        /// </summary>
        public string Mchid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string NonceStr { get; set; }
 
        /// <summary>
        /// 内容
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string OutTradeNo { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string TotalFee { get; set; }

        /// <summary>
        /// 终端IP
        /// </summary>
        public string SpbillCreateIp { get; set; }

        /// <summary>
        /// 通知url
        /// </summary>
        public string NotifyUrl { get; set; }

        /// <summary>
        /// 提交类型
        /// </summary>
        public string TradeType { get { return "JSAPI"; } }


        #region Public Methods and Operators



        #endregion

        #region Methods

        /// <summary>
        /// 验证数据是否通过
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool Va()
        {
            return true;
        }

        #endregion

        /// <summary>
        /// 获取提交url数据
        /// </summary>
        /// <returns></returns>
        public string Substring(string sign, string openid)
        {
            var d = new Dictionary<string, string>();
            d.Add("appid", this.Appid);
            d.Add("mch_id", this.Mchid);
            d.Add("nonce_str", this.NonceStr);
            d.Add("sign", sign);
            d.Add("body", this.Body);
            d.Add("out_trade_no", this.OutTradeNo);
            d.Add("total_fee", this.TotalFee);
            d.Add("spbill_create_ip", this.SpbillCreateIp);
            d.Add("notify_url", this.NotifyUrl);
            d.Add("trade_type", this.TradeType);
            d.Add("openid", openid);

           

          var data=   (WxCommon.ParseXML(d));

          return data;
        }

        /// <summary>
        /// 向服务器提交数据
        /// </summary>
        /// <returns></returns>
        public string  PostUrl(string sign, string openid)
        {
            return HttpHelper.PostPage(this.url, this.Substring(sign, openid));
        }

        public string Openid
        { get; set; }
    }
}